package model

import (
	"github.com/gorilla/websocket"
	"log"
)

type OnLineUser struct {
	UserId       int
	UserName     string
	Coon         *websocket.Conn
	ReadChannel  chan SendMessage
	WriteChannel chan SendMessage
}

func (ou *OnLineUser) Read() {
	for {
		select {
		case message, ok := <-ou.ReadChannel:
			if !ok {
				log.Printf("%s ReadService Close...\n", ou.UserName)
				return
			}
			ou.WriteChannel <- message
		}
	}
}

func (ou *OnLineUser) Write() {
	for {
		select {
		case message, ok := <-ou.WriteChannel:
			if !ok {
				log.Printf("%s WriteService Close...\n", ou.UserName)
				return
			}
			err := ou.Coon.WriteJSON(message)
			if err != nil {
				log.Printf("%sWrite Err:%s\n", ou.UserName, err.Error())
			}
		}
	}
}
